// Disable no-unused-vars, broken for spread args
/* eslint no-unused-vars: off */
import { contextBridge, ipcRenderer, IpcRendererEvent } from 'electron';

export type Channels =
  | 'ipc-example'
  | 'start-fetching-data'
  | 'start-insertDataToMysql'
  | 'data-fetching-complete'
  | 'data-insert-progress'
  | 'start-move-file'
  | 'file-move-complete'
  | 'start-fetching-houseprice-data'
  | 'houseprice-data-fetching-complete'
  | 'connect-database'
  | 'database-connect-result'
  | 'create-table'
  | 'create-table-result'
  | 'read-data-files'
  | 'read-data-result'
  | 'test-insert-data'
  | 'test-insert-result'
  | 'formal-insert-data'
  | 'formal-insert-result'
  | 'get-duplicate-data'
  | 'duplicate-data-result'
  | 'check-latest-data'  // 新增：触发检查最新数据
  | 'latest-data-result'; // 新增：返回检查结果

const electronHandler = {
  ipcRenderer: {
    sendMessage(channel: Channels, ...args: unknown[]) {
      ipcRenderer.send(channel, ...args);
    },
    on(channel: Channels, func: (...args: unknown[]) => void) {
      const subscription = (_event: IpcRendererEvent, ...args: unknown[]) =>
        func(...args);
      ipcRenderer.on(channel, subscription);

      return () => {
        ipcRenderer.removeListener(channel, subscription);
      };
    },
    once(channel: Channels, func: (...args: unknown[]) => void) {
      ipcRenderer.once(channel, (_event, ...args) => func(...args));
    },
  },
};

contextBridge.exposeInMainWorld('electron', electronHandler);

export type ElectronHandler = typeof electronHandler;
